<template>
  <div class="div">
    <div class="box">
      <h2>电商管理系统</h2>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { LoginApi } from '@/http/api'
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return false
        const res = await LoginApi(this.form)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.div {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .box {
    width: 500px;
    height: 300px;
    background-color: #fff;
    max-width: 450px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
